<template>
  <div>
    <div style="margin-bottom: 10px">

    </div>
    <div style="padding: 10px 0">
      <el-input style="width: 200px" suffix-icon="el-icon-search" placeholder="请输入名称" v-model="username"></el-input>
      <el-input style="width: 200px" suffix-icon="el-icon-message" placeholder="请输入邮箱" v-model="email"
                class="ml-5"></el-input>
      <el-input style="width: 200px" suffix-icon="el-icon-position" placeholder="请输入地址" v-model="address"
                class="ml-5"></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>
    <div style="padding: 10px 0">
      <el-button type="primary" @click="handleAdd"><i class="el-icon-plus"></i> 新增</el-button>
      <el-button type="danger" @click="isDelBatch"><i class="el-icon-delete"></i> 批量删除</el-button>
      <el-upload action="http://localhost:9090/user/import" accept=".xls, .xlsx"
                 :on-success="handleExcelImportSuccess" :on-error="handleExcelImportError"
                 :show-file-list="false" style="display: inline-block">
        <el-button type="primary" class="ml-5"><i class="el-icon-upload2"></i> 导入</el-button>
      </el-upload>
      <el-button type="primary" class="ml-5" @click="exp"><i class="el-icon-download"></i> 导出</el-button>
    </div>
    <el-table @selection-change="handleSelectionChange" :data="tableData" border stripe
              :cell-style="{'text-align':'center'}"
              :header-cell-style="{background:'#eef1f6',color:'#606266','text-align':'center'}">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="60"></el-table-column>
      <el-table-column label="头像" width="70">
        <template slot-scope="scope">
          <el-avatar :src="scope.row.avatarUrl"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="140"></el-table-column>
      <el-table-column prop="role" label="角色" width="90">
        <template slot-scope="scope">
          <el-tag type="primary" v-if="scope.row.role==='ROLE_ADMIN'">管理员</el-tag>
          <el-tag type="warning" v-if="scope.row.role==='ROLE_TEACHER'">老 师</el-tag>
          <el-tag type="success" v-if="scope.row.role==='ROLE_STUDENT'">学 生</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="nickname" label="昵称" width="120"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="phone" label="手机号"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column label="操作" width="300">
        <template v-slot="scope">
          <el-button v-if="scope.row.role === 'ROLE_TEACHER'" type="primary" @click="lookCourse(scope.row.courses)">
            <i class="el-icon-view"></i> 教授课程
          </el-button>
          <el-button v-if="scope.row.role==='ROLE_STUDENT'" type="warning" @click="myCourse(scope.row.id)">
            <i class="el-icon-view"></i> 已选课程
          </el-button>
          <el-button type="success" @click="handleEdit(scope.row)"><i class="el-icon-edit"></i> 编辑</el-button>
          <el-popconfirm class="ml-5" title="确定要删除本条记录吗？"
                         cancel-button-text='我再想想' confirm-button-text='确定'
                         icon="el-icon-info" icon-color="red"
                         @confirm="deleteById(scope.row.id)">
            <el-button type="danger" slot="reference"><i class="el-icon-delete"></i> 删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div class="pd-10">
      <el-pagination
          :total="total"
          :page-size="pageSize"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange">
      </el-pagination>
    </div>
    <!--老师课程查看-->
    <el-dialog title="教授课程信息" :visible.sync="dialogFormVisibleCourse" width="35%">
      <el-table :data="courses" border stripe :cell-style="{'text-align':'center'}"
                :header-cell-style="{background:'#eef1f6',color:'#606266','text-align':'center'}">
        <el-table-column prop="name" label="课程名称"></el-table-column>
        <el-table-column prop="score" label="课程学分"></el-table-column>
        <el-table-column prop="times" label="课程学时"></el-table-column>
      </el-table>
    </el-dialog>
    <!--学生课程查看-->
    <el-dialog title="课程信息" :visible.sync="dialogFormVisibleCourse" width="35%">
      <el-table :data="courses" :cell-style="{'text-align':'center'}" border stripe
                :header-cell-style="{background:'#eef1f6',color:'#606266','text-align':'center'}">
        <el-table-column prop="name" label="课程名称"></el-table-column>
        <el-table-column prop="score" label="课程学分"></el-table-column>
        <el-table-column prop="times" label="课程学时"></el-table-column>
      </el-table>
    </el-dialog>
    <!--编辑用户弹框-->
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="35%">
      <el-form label-width="80px" size="small">
        <el-form-item label="用户名">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select clearable v-model="form.role" placeholder="请选择角色" style="width:100%">
            <el-option v-for="item in roles" :key="item.name" :label="item.name" :value="item.flag"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
    <!--批量删除弹框-->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span><i class="el-icon-delete-solid" style="font-size: 35px"></i>  确定要删除这些用户吗？</span>
      <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="delBatch">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "UserManage",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      username: "",
      email: "",
      address: "",
      dialogFormVisible: false,
      dialogFormVisibleCourse: false,
      form: {},
      multipleSelection: [],
      dialogVisible: false,
      roles: [],
      courses: []
    }
  },
  created() {
    this.load()
  },
  methods: {
    lookCourse(courses) {
      this.courses = courses
      this.dialogFormVisibleCourse = true
    },
    myCourse(id) {
      this.request.get("/studentCourse/stuCourse/" + id).then(res => {
        if (res.code === '200') {
          console.log(res.data)
          this.courses = res.data
        } else {
          this.$message.error("操作异常")
        }
      })
      this.dialogFormVisibleCourse = true
    },
    load() {
      this.request.get("/user/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          username: this.username,
          email: this.email,
          address: this.address
        }
      }).then(res => {
        console.log(res.data)
        this.total = res.data.total
        this.tableData = res.data.records
      });
      this.request.get("/role").then(res => {
        this.roles = res.data
      })
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    reset() {
      this.username = ""
      this.email = ""
      this.address = ""
      this.load()
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    save() { //保存信息
      this.dialogFormVisible = false
      this.request.post("/user/save", this.form).then(res => {
        if (res.code === '200') {
          this.$message.success("保存成功！")
          this.dialogFormVisible = false
        } else {
          // this.$message.error("保存失败，请重试！")
          this.$message.error(res.msg)
        }
        this.load()
      })
    },
    handleEdit(row) { //修改信息
      this.form = row
      this.dialogFormVisible = true
    },
    deleteById(id) { //根据id删除用户
      console.log("要删除的id:" + id)
      this.request.delete("/user/" + id).then(res => {
        if (res.code === '200') {
          this.$message.success("删除成功！")
          this.load()
        } else {
          this.$message.error("操作失败，请重试！")
        }
      })
    },
    handleSelectionChange(val) { //批量删除前的选择
      console.log(val) //val是一个数组对象，存放着每条记录的要展示的详细信息
      this.multipleSelection = val
    },
    isDelBatch() {
      this.dialogVisible = true
    },
    delBatch() { //批量删除
      let ids = this.multipleSelection.map(value => value.id) //从数组对象中获取到每一个的id项
      this.request.post("/user/del/batch", ids).then(res => {
        if (res.code === '200') {
          this.$message.success("批量删除成功！")
          this.load()
        } else {
          this.$message.error("操作失败，请重试！")
        }
        this.dialogVisible = false
      })
    },
    exp() {
      window.open("http://localhost:9090/user/export")
    },
    handleExcelImportSuccess(res) {
      if (res.code === '200') {
        this.$message.success("导入成功")
      } else {
        this.$message.error(res.msg)
      }
      this.load()
    },
    handleExcelImportError() {
      this.$message.error("操作异常，请重试")
      this.load()
    }
  }
}
</script>

<style scoped>

</style>